import request from '@/utils/request'
import {useUserStore} from '@/store/user'

// 记录浏览历史
export const recordBrowseHistory = async (serviceId) => {
  const userStore = useUserStore()
  if (!userStore.isLoggedIn || !userStore.userInfo?.id) {
    return Promise.resolve() // 未登录或没有用户ID时直接返回
  }
  
  try {
    return await request.post('/browse-history', null, {
      params: {
        userId: userStore.userInfo.id,
        serviceId: serviceId
      },
      showDefaultMsg: false // 不显示默认的成功/错误提示
    })
  } catch (error) {
    console.error('记录浏览历史失败:', error)
    throw error
  }
}

// 清除浏览历史
export function clearBrowseHistory() {
  const userStore = useUserStore()
  return request.delete(`/browse-history?userId=${userStore.userInfo.id}`,{
    showDefaultMsg:false
  })
}

// 获取浏览历史列表
export function getBrowseHistoryList(params) {
  const userStore = useUserStore()
  return request.get('/browse-history/list', {
    ...params,
    userId: userStore.userInfo.id
  },null,{
    showDefaultMsg:false
  })
}

// 获取浏览历史详情
export function getBrowseHistoryDetail(id) {
  const userStore = useUserStore()
    return request.get(`/browse-history/${id}?userId=${userStore.userInfo.id}`,null,{
      showDefaultMsg:false
    })
} 